#!/usr/bin/python
# -*- coding: utf-8 -*-

import sys
import yaml
import MySQLdb

import MySQLdb.cursors 
import numpy as np
from sklearn.feature_extraction.text import TfidfTransformer

import csv
#EFETUA LIGACAO
def db_establish_conn(yaml_options):
    try:
        database = MySQLdb.connect(host=yaml_options['Database']['host'],user=yaml_options['Database']['user'],passwd=yaml_options['Database']['pass'],db=yaml_options['Database']['dbname'])#,cursorclass=MySQLdb.cursors.DictCursor)
        cur = database.cursor()
        return database, cur

    except MySQLdb.Error, e:
        print "Error %d: %s" % (e.args[0],e.args[1])
        sys.exit(1)

#LISTA BD
def list_it(yaml):
    transformer = TfidfTransformer() #pertence ao sklearn

    (db, cursor) = db_establish_conn(yaml)
    sql = "SELECT * FROM filejson LIMIT 5"

    try:
        cursor.execute(sql)
        rows = cursor.fetchall() #isto nao é necessario para criar o csv

    except Exception, e:
        print e

    with open("out.csv", "wb") as csv_file:
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow([i[0] for i in cursor.description]) # write headers
        csv_writer.writerows(cursor)  

    #sep1 = ' '
    #sep2 = ','
    #f = open('out.csv')
    #data = np.loadtxt('out.csv', delimiter=',')
    #x=[]
    #y=[]
    #for line in f:
    #    splits  =  line.strip().split('\n')[0].strip().split(sep1)
    #    x.append( [float(el)  for el in splits[1].strip().split(',')])
    #    y.append(splits[0].strip())

    #X= np.array(x)
    #Y=np.array(y)

#    for row in rows:
#        x= []
#        x.append(row['time_received'])
#        x.append(row['ipver'])
#        x.append(row['srcip'])
#        y.append(x)
#    X= np.array(y)   
    #tfidf = transformer.fit_transform(X)
    #tfidf
    #print X

    print "//////////////////////////////////////////////////////////////////////////////////////////////"

    #print Y
    print data
    #tfidf.toarray()

#PARSE DO FICHEIRO YAML
class ParseYamlConfig():
    def parse(self):
	try:
            f = open('db.yaml', 'r')
	except IOError as e:
	    print "I/O error({0}): {1}".format(e.errno, e.strerror), "ERROR !!!"
		
	self.dataMap = yaml.load(f)
	f.close()
        
        return self.dataMap;


def main():    
    yaml_options = ParseYamlConfig().parse()

    list_it(yaml_options)

 
if __name__ == "__main__":
    main()
